﻿/**
 *	@file		CCGemetry.cpp
 *	@author		NeoZhang
 *	@version	v0.1
 *	@date		2013/09/03
 *	@brief		几何(坐标、尺寸、矩形)处理实现
 */

#include "CCGeometry.h"

NS_CC_BEGIN

/**
 *	CCPoint实现
 */

CCPoint::CCPoint(void)
{
	setPoint(0.0f, 0.0f);
}

CCPoint::CCPoint(float x, float y)
{
	setPoint(x, y);
}

CCPoint::CCPoint(const CCPoint& other)
{
	setPoint(other.x, other.y);
}

CCPoint& CCPoint::operator= (const CCPoint& other)
{
	setPoint(other.x, other.y);
	return *this;
}

void CCPoint::setPoint(float x, float y)
{
	this->x = x;
	this->y = y;
}

bool CCPoint::equals(const CCPoint& target) const
{
	return ((x == target.x) && (y == target.y));
}

/**
 *	CCSize实现
 */

CCSize::CCSize(void)
{
	setSize(0.0f, 0.0f);
}

CCSize::CCSize(float width, float height)
{
	setSize(width, height);
}

CCSize::CCSize(const CCSize& other)
{
	setSize(other.width, other.height);
}

CCSize& CCSize::operator= (const CCSize& other)
{
	setSize(other.width, other.height);
	return *this;
}

void CCSize::setSize(float width, float height)
{
	this->width = width;
	this->height = height;
}

bool CCSize::equals(const CCSize& target) const
{
	return ((width == target.width) && (height == target.height));
}

/**
 *	CCRect实现
 */

CCRect::CCRect(void)
{
	setRect(0.0f, 0.0f, 0.0f, 0.0f);
}

CCRect::CCRect(float x, float y, float width, float height)
{
	setRect(x, y, width, height);
}

CCRect::CCRect(const CCRect& other)
{
	setRect(other.origin.x, other.origin.y, other.size.width, other.size.height);
}

CCRect& CCRect::operator= (const CCRect& other)
{
    setRect(other.origin.x, other.origin.y, other.size.width, other.size.height);
    return *this;
}

void CCRect::setRect(float x, float y, float width, float height)
{
    // CGRect can support width<0 or height<0
    // CCAssert(width >= 0.0f && height >= 0.0f, "width and height of Rect must not less than 0.");

    origin.x = x;
    origin.y = y;

    size.width = width;
    size.height = height;
}

bool CCRect::equals(const CCRect& rect) const
{
    return (origin.equals(rect.origin) && 
            size.equals(rect.size));
}

float CCRect::getMaxX() const
{
    return (float)(origin.x + size.width);
}

float CCRect::getMidX() const
{
    return (float)(origin.x + size.width / 2.0);
}

float CCRect::getMinX() const
{
    return origin.x;
}

float CCRect::getMaxY() const
{
    return origin.y + size.height;
}

float CCRect::getMidY() const
{
    return (float)(origin.y + size.height / 2.0);
}

float CCRect::getMinY() const
{
    return origin.y;
}

bool CCRect::containsPoint(const CCPoint& point) const
{
    bool bRet = false;

    if (point.x >= getMinX() && point.x <= getMaxX()
        && point.y >= getMinY() && point.y <= getMaxY())
    {
        bRet = true;
    }

    return bRet;
}

bool CCRect::intersectsRect(const CCRect& rect) const
{
    return !(     getMaxX() < rect.getMinX() ||
             rect.getMaxX() <      getMinX() ||
                  getMaxY() < rect.getMinY() ||
             rect.getMaxY() <      getMinY());
}


NS_CC_END